package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.BindFamilyDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.FamilyElderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import static com.zzyl.common.utils.UserThreadLocal.getUserId;

@Service
@Slf4j
public class FamilyMemberElderServiceImpl implements IFamilyMemberElderService {
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;


    public FamilyMemberElderServiceImpl(ElderMapper elderMapper) {
        super();
        this.elderMapper = elderMapper;
    }
    /**
     * 添加家属
     * */
    @Override
    public void add(BindFamilyDto dto) {
        Elder elder = elderMapper.selectOne(
                new LambdaQueryWrapper<Elder>()
                        .eq(Elder::getIdCardNo, dto.getIdCard())
                        .eq(Elder::getName, dto.getName()));
        if (Objects.isNull( elder)){
            throw new ServiceException("没有该老人数据");
        }
        Long userId=getUserId();
        FamilyMemberElder familyMemberElder = new FamilyMemberElder();
        familyMemberElder.setElderId(elder.getId());
        familyMemberElder.setFamilyMemberId(userId);
        familyMemberElder.setRemark(dto.getRemark());
        familyMemberElderMapper.insert(familyMemberElder);
    }

    /**
     * 查询我的家人（老人）列表
     * */

    @Override
    public List<FamilyMemberElder> getMyElders() {
        Long userId = getUserId();
        log.info("当前用户 ID: {}", userId);
        return familyMemberElderMapper.selectByUserId(userId);
    }
    /**
     * 查看已绑定家人列表（分页查询)
     * */
    @Override
    public List<FamilyElderVo> listByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Long userId = getUserId();
        List<FamilyElderVo> list = familyMemberElderMapper.selectFamilyElderList(userId);
        return list;
    }


    @Override
    public boolean saveBatch(Collection<FamilyMemberElder> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<FamilyMemberElder> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<FamilyMemberElder> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(FamilyMemberElder entity) {
        return false;
    }

    @Override
    public FamilyMemberElder getOne(Wrapper<FamilyMemberElder> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<FamilyMemberElder> queryWrapper) {
        return Map.of();
    }

    @Override
    public <V> V getObj(Wrapper<FamilyMemberElder> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<FamilyMemberElder> getBaseMapper() {
        return null;
    }

    @Override
    public Class<FamilyMemberElder> getEntityClass() {
        return null;
    }

}
